package com.ypj.indusynergyems.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ypj.indusynergyems.dao.entity.*;
import com.ypj.indusynergyems.dao.mapper.*;
import com.ypj.indusynergyems.service.IShopService;
import com.ypj.indusynergyems.toolkit.RandomGenerator;
import lombok.RequiredArgsConstructor;
import org.apache.catalina.User;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ShopServiceImpl extends ServiceImpl<ShopMapper, ShopEntity> implements IShopService {

    private final UserMapper userMapper;

    private final UserShopMapper userShopMapper;

    private final GoodsMapper goodsMapper;

    private final GoodsTypeMapper goodsTypeMapper;

    private final ShopMapper shopMapper;

    @Override
    public List<ShopEntity> findAllShopById(int userId) {
        LambdaQueryWrapper<ShopEntity> queryWrapper = Wrappers.lambdaQuery(ShopEntity.class)
                .eq(ShopEntity::getUserId, userId)
                .eq(ShopEntity::getStatus, 0);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public String shopRegister(int userId, ShopEntity shop) {
        String shopNum;
        do {
            shopNum = RandomGenerator.generateRandom();
        }while (!getShopNum(shopNum));
        shop.setShopNum(shopNum);
        shop.setShopPlatformApply(0);
        UserShopEntity userShop = UserShopEntity.builder()
                .shopId(shop.getShopId())
                .userId(userId)
                .build();
        userShopMapper.insert(userShop);
        return baseMapper.insert(shop)>0?"success":"fail";
    }

    @Override
    public List<GoodsEntity> findAllGoodsByShopId(int shopId) {
        LambdaQueryWrapper<GoodsEntity> queryWrapper = Wrappers.lambdaQuery(GoodsEntity.class)
                .eq(GoodsEntity::getShopId, shopId);
        return goodsMapper.selectList(queryWrapper);
    }

    @Override
    public String getShopTypeName(GoodsEntity goodsEntity) {
        return goodsTypeMapper.selectTypeName(goodsEntity);
    }

    @Override
    public String findShopName(int shopId) {
        return baseMapper.findShopName(shopId);
    }

    @Override
    public List<GoodsTypeEntity> findAllShopType(int shopId) {
        LambdaQueryWrapper<GoodsTypeEntity> queryWrapper = Wrappers.lambdaQuery(GoodsTypeEntity.class)
                .eq(GoodsTypeEntity::getShopId, shopId);
        return goodsTypeMapper.selectList(queryWrapper);
    }

    @Override
    public String addGoodsType(GoodsTypeEntity goodsType) {
        return goodsTypeMapper.insert(goodsType)>0?"success":"fail";
    }

    @Override
    public List<GoodsTypeEntity> findAllGoodsType() {
        return new ArrayList<>();
    }

    @Override
    public List<UserEntity> findAllShopAdmin(int shopId) {
        //1. 根据店铺Id 查询管理员信息
        LambdaQueryWrapper<UserShopEntity> queryWrapper = Wrappers.lambdaQuery(UserShopEntity.class)
                .eq(UserShopEntity::getShopId, shopId);

        List<UserShopEntity> userShopEntities = userShopMapper.selectList(queryWrapper);

        if (userShopEntities == null) {
            throw new RuntimeException("查询店铺管理员信息时发生错误");
        }

        List<Integer> userIds = userShopEntities.stream().map(UserShopEntity::getUserId).toList();

//2. 根据用户Id 查询管理员信息
        List<UserEntity> userList = Collections.emptyList();
        if (!userIds.isEmpty()) {
            try {
                userList = userMapper.selectByIds(userIds);
                System.out.printf(userList.toString());
            } catch (Exception e) {
                throw new RuntimeException("查询用户信息时发生错误", e);
            }
        }
        return userList;
    }

    public boolean getShopNum(String shopNum){
        LambdaQueryWrapper<ShopEntity> queryWrapper = Wrappers.lambdaQuery(ShopEntity.class)
                .eq(ShopEntity::getShopNum, shopNum)
                .eq(ShopEntity::getStatus, 0);
        ShopEntity hasNumFlag = baseMapper.selectOne(queryWrapper);
        return hasNumFlag == null;
    }


    @Override
    public ShopEntity findShopDetail(int shopId) {
        return shopMapper.findShopDetail(shopId);
    }


    @Override
    public String editShop(ShopEntity shop) {
        return shopMapper.editShop(shop)>0?"success":"fail";
    }

    @Override
    public String deleteShop(int shopId) {
        return shopMapper.deleteShop(shopId)>0?"success":"fail";
    }

}
